/**   
* @Title: CarstockController.java 
* @Package com.hxqc.erpsearch.carstock.controller 
* @Description: TODO(用一句话描述该文件做什么) 
* @author wanglei 
* @date 2016-7-7 上午11:38:41 
*/
package com.example.caoh.common.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.example.caoh.common.mapper.ImportDataMapper;
import com.example.caoh.common.util.Config;
import com.example.caoh.common.util.ElsIndex;
import com.example.caoh.common.util.ElsRequest;
import com.example.caoh.common.util.StringUtil;

/**
 * 鑫安索引重导数据接口
 * @ClassName: InterfaceController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author GUPENG
 * @date 2018年6月16日 上午9:09:10
 */
@RestController
@RequestMapping("/esImport")
public class InterfaceController {
	@Resource	
	private ImportDataMapper importDataMapper;
	
	/**
	 * 批量创建索引
	 * @Description: 
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "/createIndex")
	@ResponseBody
	public static void createIndex() throws Exception {
        String path = InterfaceController.class.getClassLoader().getResource("2.3").getPath();
        File file = new File(path);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                InputStreamReader reader = new InputStreamReader(new FileInputStream(files[i]));
                BufferedReader br = new BufferedReader(reader);
                StringBuilder stringBuilder = new StringBuilder();
                String line = "";
                line = br.readLine();
                stringBuilder.append(line);
                while (line != null) {
                    line = br.readLine();
                    stringBuilder.append(line);
                }
                String mapping = stringBuilder.toString();
                ElsRequest.create(Config.getConfig("ELS_URL"), files[i].getName().replace(".txt", ""), mapping);
                br.close();
            }
        }
    }
	
	/**
	 * 1.客户信息
	 * @Description: 
	 * @param entity
	 * @return
	 * @return Object
	 */
	@RequestMapping(value = "/importTestData")
	@ResponseBody
	public Object importCustomers(@RequestParam(value = "startTime", required = false) String startTime,@RequestParam(value = "endTime", required = false) String endTime) {
		try{
			String url = Config.getConfig("ELS_URL") + "/" + ElsIndex.TESTDATA;
			long total = 0;
				int rows = 10000;
				long start = 0;
				List<Map<String, Object>> list = importDataMapper.findTestData(start, rows,startTime,endTime);
				StringBuilder bulk = new StringBuilder();
				while (list.size() > 0) {
					bulk.delete(0, bulk.length());
					for (Map<String, Object> map : list) {
						//使用map封装索引主键ID，否则带有特殊字符主键无法插入索引，导致数据导入停止。
						Map<String, Object> _index = new HashMap<String, Object>();
						Map<String, Object> _id = new HashMap<String, Object>();
						_id.put("_id", "test" +map.get("id")+ map.get("phone"));
						_index.put("index", _id);
						bulk.append(JSONObject.toJSON(_index).toString()).append("\n");
						bulk.append(JSONObject.toJSON(map).toString()).append("\n");
					}
					ElsRequest.bulk(url, bulk.toString());
					start += rows;
					if (list.size() < rows) break;
					list = importDataMapper.findTestData(start, rows,startTime,endTime);
				}
				total += list.size();
			return  "客户信息数据导入ES成功:"+total+"条数据";
		}catch(Exception e){
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	
}
